package demo.bean;

import java.io.Serializable;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Set;

import demo.entrymain.EntryMain;
import demo.factor.Ifactor.IFactory;
import demo.factor.PeoFactory.PeoFactory;

public class Admin implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8690976956344718821L;
	private String number;
	private String password;
	private String name="wp";
	
	public Admin(String number,String password)
	{
		this.number=number;
		this.password=password;
		this.name="wp";
	}
	
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	
	@Override
	public String toString ()
	{
		return "number:"+number+"  password:"+password+"  name:"+name;
		
	}
	
	//登录
	public static void adminLogin()
	{
		System.out.println("---------------管理员登录--------------------");
		Scanner in=new Scanner(System.in);
		System.out.println("请输入工号：");
		String number=in.next();
		System.out.println("请输入密码：");
		String pass=in.next();
		if(number.equals(PeoFactory.fa.readAdmin().getNumber())&&pass.equals(PeoFactory.fa.readAdmin().getPassword()))
		{
			System.out.println("-----------------------------------------------------------");
			System.out.println("欢迎管理员："+PeoFactory.fa.readAdmin().getName()+"    登陆成功");
			managerOpreation();
		}else{
			System.err.println("登陆失败    (1)重新登录?  or (2)重新选择登录方式?");
			adminLoginCheck();
			
		}
	}
	public static void adminLoginCheck()
	{
		Scanner in=new Scanner(System.in);
		while(true)
		{
			String a=in.next();
			try{
					int x = Integer.parseInt(a);
					switch(x)
					{
						case 1:
							adminLogin();break;
						case 2:
							EntryMain.mainMenu();
							EntryMain.start();break;
						default:
							System.err.println("输错数字了重新输入：");
							adminLoginCheck();
					}	
			}catch(NumberFormatException e){
				System.err.println("类型不匹配，请重新输入");
				}
		}
	}
	
	
	//管理员界面
	public static void managerOpreation()
	{
		System.out.println("-----------------------管理员界面------------------------------");
		System.out.println("(1)商品上架      (2)修改商品    (3)查看所有商品   (4)退出登录回到主界面");
		System.out.println("请输入编号：");
		Scanner in=new Scanner(System.in);
		
		while(true){
            String str = in.next();
            try {
                int x = Integer.parseInt(str);
                switch(x)
    			{
    				case 1:
    					addTheGoods();break;
    				case 2:
    					elertTheGoods();break;
    				case 3:
    					lookForAllGoods();break;	
    				case 4:
    					EntryMain.mainMenu();
    					EntryMain.start();break;	
    				default:
    					System.err.println("输错数字了重新输入：");
    			}
            } catch (NumberFormatException e) {
                System.err.println("类型不匹配，请重新输入");
                
            }
        }
	}
	
	
	
	
	//商品上架
	public static void addTheGoods()
	{
		String name = null;
		double price = 0;
		int number = 0;
		String cer = null;
		Scanner in=null;
		while(true){
			try{
				in=new Scanner(System.in);
				System.out.println("--------商品上架--------");
				System.out.println("输入商品的名字：");
				name=in.next();
				System.out.println("输入商品的价格(double)：");
				price=in.nextDouble();
				System.out.println("输入商品的数量(int)：");
				number=in.nextInt();
				System.out.println("确认添加？（y）");
				cer=in.next();
			}catch(Exception e){
				System.err.println("输入错误！重新输入");
				continue;
				//e.printStackTrace();
			}
			if(cer.equals("y"))
			{
				Goods g=new Goods(name,price,number);
				if(IFactory.addAGoods(g))
				{
					System.out.println("添加成功");
					managerOpreation();break;
				}else{
					System.err.println("有重复！添加失败   1重新添加?  or  2返回管理员页面");
					switch(in.nextInt())
					{
						case 1:
							continue;
						case 2:
							managerOpreation();break;
						default:
							System.err.println("输错数字了重新输入：");
							continue;
					}
					
				}
				
			}else{
				System.err.println("没添加");
				
			}
		}
		
	}
	//修改商品
	public static void elertTheGoods()
	{
		Scanner in=new Scanner(System.in);
		Goods good=null;
		System.out.println("--------修改商品--------");
		System.out.println("输入商品id：");
	     while(true)
	     {
	    	 String a=in.next();
	    	 int id=0;
	    	 try{
	    		 id=Integer.parseInt(a);
	    	 	}catch(Exception e){
	    	 		System.err.println("请输入数字！");
	    	 	}
	    	 
	    		 //id 查找商品
		    	 good=IFactory.selectAGoods(id);
		    	 //检查good是否为空
		    	 if(good==null)
		    	 {
		    		 System.err.println("找不到商品，请重新输入id：");
		    		 continue;
		    	 }else{
		    		//得到good的id
		    		int a1=good.getGoodsId();
		    		System.out.println("==============="+a1);
		 	    	System.out.println(good);
		 	    	//执行修改
		 	    	executeAlertGoods(good);
		 	    	break;
		    	 }	 
	    		 
	    	
	    	 
	     }
	     
	     
	}
	//执行修改
	public static void executeAlertGoods(Goods good)
	{	
		System.out.println("修改商品    (1)数量   (2)名字<不能重复！>");
		Scanner in=new Scanner(System.in);
		while(true)
		{
			String str1 = in.next();
			try {
                int x = Integer.parseInt(str1);
                if(x==1)
                {
                	System.out.println("输入商品数量：");
					good.setNumber(in.nextInt());
					break;
                }else if(x==2){
                	System.out.println("输入商品名字：");
					good.setName(in.next());
					break;
                }else{
                	System.err.println("序号错误,重新输入：");
                }
			    
			}catch (NumberFormatException e) {
				System.err.println("请输入序号");
            }
		}
		System.out.println("确认修改？（y）");
		String a=in.next();
		if(a.equals("y"))
		{
 			boolean a1=IFactory.alertGoods(good);
 			System.out.println("=============="+a1);
 			if(a1)
 			{
	    			System.out.println("修改成功  (1)还要修改商品？  (2)返回管理员页面");	
	    			
	    			int h=0;
	    			while(true)
	    			{
	    				String g=in.next();
	    				try{
	    					h=Integer.parseInt(g);
	    				}catch(Exception e){
	    					System.err.println("输入数字：");
	    					continue;
	    				}
	    				
	    				if(h==1)
	    				{
	    					elertTheGoods();break;
	    				}else if(h==2){
	    					managerOpreation();break;
	    				}else{
	    					System.err.println("输入正确的数字：");
	    				}
	    			}
 			}
 			
 			
 		}else{
 			System.err.println("没有修改");
 			elertTheGoods();
 		}
		
	}
	//查找所有商品
	public static void lookForAllGoods()
	{
		Scanner in=new Scanner(System.in);
		Set<Goods> list =IFactory.selectAllGoods();
		System.out.println(list);
		System.out.println("任意键返回");
		in.next();
		managerOpreation();	
	}
}
